package dalex.command;

import java.io.IOException;
import java.sql.* ;

import dalex.server.Common;

public class RegistCommand extends Command{
	public final String name = "registe" ;

	public void  action() {
		String username = this.command.split(" ")[1] ; 
		String nickname = this.command.split(" ")[2] ;
		String password = this.command.split(" ")[3] ; 
		
		System.out.println("registCommand action:") ;
		System.out.println(username) ;
		System.out.println(nickname) ; 
		System.out.println(password) ; 
		
		try {
			String sql1 = new String( "select * from user where username='"+username+"'" ) ;
			String sql2 = new String( "select * from user where nickname='"+nickname+"'" ) ; 
			String sql3 = new String( "insert into user (username,nickname,password) values ('"+username+"','"+nickname+"','"+password+"')") ; 
			
			System.out.println(sql3) ; 
			
			Common.resultSet = Common.statement.executeQuery( sql1 );
			if( Common.resultSet.next()){
				this.out.writeObject("REGISTE_USERNAME_EXIST");
				this.out.flush() ; 
				System.out.println("已存在该用户") ;
			}else{
				Common.resultSet = Common.statement.executeQuery(sql2) ;
				if( Common.resultSet.next() ){
					this.out.writeObject("REGISTE_NICKNAME_EXIST");
					this.out.flush(); 
					System.out.println("昵称已被占用") ;
				}else{
					Common.statement.execute(sql3) ; 
					this.out.writeObject("REGISTE_SUCCEEDED") ;
					this.out.flush() ; 
					System.out.println("注册成功") ; 
				}				
			}			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch( IOException ioe ){
			ioe.printStackTrace();
		}
		
	}
}
